import express from 'express'
import http from 'http'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
import compression from 'compression'
import cors from 'cors'
import mongoose from 'mongoose'

import router from './router'

const app = express()

app.use(cors({
  credentials: true
}))

app.use(compression())
app.use(cookieParser())
app.use(bodyParser.json())

const server = http.createServer(app)
server.listen(3001, () => {
  console.log('server is running at port 6666 : http://localhost:3001')
})

const MONGO_DB = 'mongodb://halcyon:halcyon@localhost:27017/?authMechanism=DEFAULT&authSource=admin'

mongoose.Promise = Promise
mongoose.connect(MONGO_DB)
mongoose.connection.on('connection error', (error: Error) => { console.log(error) })

app.use('/', router())
